package com.src;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

import com.src.model.Experience;
import com.src.model.User;

@Path("/userinfo")
public class UserService {

	private String error_msg;
	
	@GET
	@Path("/isInFavorites")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String isInFavorites(@QueryParam("username") String uname, @QueryParam("favoriteuser") String favUser){
	
		Boolean result = false;

		try {
			result = DBConnection.isInFavorites(uname, favUser);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result == null || result == false){
			return Utitlity.constructJSON("false", "no it is not");
		} else {
			return Utitlity.constructJSON("true", "yes it is");
		}	
	}

	@GET
	@Path("/addToFavorites")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String addToFavorite(@QueryParam("username") String uname, @QueryParam("favoriteuser") String favUser){
		Boolean result = false;

		try {
			System.out.println("hasan");
			result = DBConnection.saveFavorite(uname, favUser);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result == null || result == false){
			return Utitlity.constructJSON("false", "Internal System Error");
		} else {
			return Utitlity.constructJSON("true", "");
		}
	}
	
	@GET
	@Path("/removeFromFavorites")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String removeFromFavorite(@QueryParam("username") String uname, @QueryParam("favoriteuser") String favUser){
		Boolean result = false;

		try {
			result = DBConnection.removeFavorite(uname, favUser);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result == null || result == false){
			return Utitlity.constructJSON("false", "Internal System Error");
		} else {
			return Utitlity.constructJSON("true", "");
		}
	}
	
	@GET
	@Path("/updateAvatar")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String updateAvatar(@QueryParam("username") String uname, @QueryParam("url") String url){
		Boolean result = false;

		try {
			result = DBConnection.updateAvatar(uname, url);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(result == null || result == false){
			return Utitlity.constructJSON("false", "Internal System Error");
		} else {
			return Utitlity.constructJSON("true", "");
		}

	}

	@GET
	@Path("/getUserInfo")
	@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
	public String getUserInfo(@QueryParam("username") String uname) {
		String result = null;
		User user = null;
		if (Utitlity.isNotNull(uname)) {
			try {
				user = DBConnection.getUserInfo(uname);

				result = generateJson(user);
			} catch (Exception e) {
				error_msg = "Database exception :\n" + e.toString();
				result = Utitlity.constructJSON("false", error_msg);
				;
			}
		} else {
			error_msg = "Username cannot be empty!";
			result = Utitlity.constructJSON("false", error_msg);
			;
		}
		return result;
	}

	private String generateJson(User user) {
		JSONObject obj = new JSONObject();
		if (user == null) {
			return Utitlity.constructJSON("0", "There is no such an User!");
		} else {
			try {
				obj.put("status", new Boolean("true"));
				obj.put("firstname", user.getFirstName());
				obj.put("lastname", user.getLastName());
				obj.put("email", user.getEmail());
				obj.put("avatarLink", user.getAvatarLink());
				JSONArray ja = new JSONArray();
				for (int i = 0; i < user.getExperienceList().size(); i++) {
					JSONObject jo = new JSONObject();
					Experience input = user.getExperienceList().get(i);
					jo.put("tag", input.getTags());
					jo.put("body", input.getText());
					jo.put("username", input.getCreatedUserName());
					jo.put("experienceID", input.getID());
					jo.put("helpfulCount", input.getHelpful());
					jo.put("unhelpfulCount", input.getUnhelpful());
					jo.put("imageURL", input.getImageURL());
					jo.put("spamCount", input.getSpam());
					jo.put("latitude", input.getLatitude());
					jo.put("longitude", input.getLongitude());
					jo.put("exact", input.getExact());
					ja.put(jo);
				}
				obj.put("experiences", ja);
			} catch (JSONException e) {
				System.out.println("Json parse exception" + e.toString());
			}
			return obj.toString();
		}

	}
}